package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.EmailBean;
import com.project.bean.InboxBean;
import com.project.mapper.IEmailMapper;
import com.project.mapper.IInboxMapper;
import com.project.service.IEmailService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
@Service
@Transactional
public class IEmailServiceImpl implements IEmailService {
    @Autowired
    private IEmailMapper emailMapper;
    @Override
    public void send(EmailBean email, Integer[] receiverIdArray) {
        emailMapper.insert(email);
        emailMapper.addReceivers(email,receiverIdArray);
    }

    @Override
    public IPage<EmailBean> cutByItem(Integer pageNO, Integer senderId, String head, LocalDate startDate, LocalDate endDate) {
        QueryWrapper<EmailBean> qw = new QueryWrapper<>();
        qw.eq("fk_senderId",senderId);
        qw.eq("e_status","有效");
        if (head!=null&&!"".equals(head)){
            qw.like("e_head",head);
        }
        if (startDate!=null){
            qw.ge("e_date",startDate);
        }
        if (endDate!=null){
            qw.le("e_date",endDate);
        }
        return emailMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public EmailBean findById(Integer emailId) {
        return emailMapper.selectById(emailId);
    }

    @Override
    public void del(Integer emailId) {
        EmailBean email = emailMapper.selectById(emailId);
        email.setStatus("无效");
         emailMapper.updateById(email);
    }
}
